//
//  XWUILabelExtension.swift
//  XiaoweHearingAid
//
//  Created by Riven on 2023/8/23.
//

import Foundation
import UIKit


/**
 #import "UILabel+ChangeLineSpaceAndWordSpace.h"

 @implementation UILabel (ChangeLineSpaceAndWordSpace)

 + (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {

     NSString *labelText = label.text;
     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
     [paragraphStyle setLineSpacing:space];
     [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
     label.attributedText = attributedString;
     [label sizeToFit];

 }

 + (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {

     NSString *labelText = label.text;
     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];
     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
     [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
     label.attributedText = attributedString;
     [label sizeToFit];

 }

 + (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {

     NSString *labelText = label.text;
     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];
     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
     [paragraphStyle setLineSpacing:lineSpace];
     [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
     label.attributedText = attributedString;
     [label sizeToFit];

 }

 @end
 */

public extension UILabel {
    
    func setLineSpace(_ lineSpace: CGFloat) {
        guard let labelText = text else {
            return
        }
        
        let attributedString = NSMutableAttributedString(string: labelText)
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        paragraphStyle.alignment = textAlignment
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: labelText.count))
        attributedText = attributedString
//        sizeToFit()
    }
    
}
